using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroEquipWearEquipsInfo : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_HeroEquipWearEquipsInfo
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject attribute { protected set; get; }
            public UnityEngine.GameObject skillMould { protected set; get; }
            public UnityEngine.GameObject noAttr { protected set; get; }
            public UnityEngine.GameObject noSkill { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attribute", out var __tbv0);
                this.attribute = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("skillMould", out var __tbv1);
                this.skillMould = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("noAttr", out var __tbv2);
                this.noAttr = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("noSkill", out var __tbv3);
                this.noSkill = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private ListScrollAdapter<UIN_CommonIconTitleValue> attributes = null;
        private ListScrollAdapter<UIN_CommonTitleValue> skills = null;

        private HeroData heroData = null;
        private Dictionary<int, int> dicAttributeData = new(4);
        private List<int> listAttributeData = ListPool<int>.Get();
        private List<int> listSkillData = ListPool<int>.Get();

        #endregion fields

        #region properties

        protected UIB_UIN_HeroEquipWearEquipsInfo ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroEquipWearEquipsInfo();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.attributes = this.InitListScroll<UIN_CommonIconTitleValue>(this.ui.attribute, this.OnAttributeChanged);
            this.skills = this.InitListScroll<UIN_CommonTitleValue>(this.ui.skillMould, this.OnSkillNameDescChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.Clear();
        }

        #region methonds

        private void Clear()
        {
            this.heroData = null;

            if (this.listAttributeData != null)
            {
                ListPool<int>.Put(this.listAttributeData);
            }
            this.listAttributeData = null;
            if (this.listSkillData != null)
            {
                ListPool<int>.Put(this.listSkillData);
            }
            this.listSkillData = null;
        }

        public void Show(HeroData heroData)
        {
            this.Show();
            this.heroData = heroData;

            this.RefreshShow();
        }

        public void RefreshShow()
        {
            this.ReInitEquipData();
            this.ShowAttribute();
            this.ShowSkillNameDesc();
        }

        private void ReInitEquipData()
        {
            this.dicAttributeData.Clear();
            this.listAttributeData.Clear();
            this.listSkillData.Clear();

            HashSet<int> hsSkillCid = new(4);
            foreach (var equipSvId in this.heroData.equips.Values)
            {
                ItemEquipmentData equipItem = ItemModule.Instance.GetItemById(equipSvId) as ItemEquipmentData;
                if (equipItem != null)
                {
                    this.CalcEquipAttribute(equipItem, this.dicAttributeData);
                    this.DoStatisticsEquipSkill(equipItem, hsSkillCid);
                }
            }


            this.listAttributeData.AddRange(this.dicAttributeData.Keys);
            this.listSkillData.AddRange(hsSkillCid);
        }

        private void CalcEquipAttribute(ItemEquipmentData equipItem, Dictionary<int, int> dicAttribute)
        {
            var dic = AttributeDataUtils.GetEquipAllAttributeDic(equipItem, GameDataCenter.PlayerData.equipSkins);
            foreach (var attrCidValue in dic)
            {
                int attrCid = attrCidValue.Key;
                dicAttribute.TryGetValue(attrCid, out var attrValue);
                dicAttribute[attrCid] = attrCidValue.Value + attrValue;
            }
        }

        private void DoStatisticsEquipSkill(ItemEquipmentData equipData, HashSet<int> hsSkillCid)
        {
            var cfgStar = equipData.cfgStar;
            int star = Math.Min(equipData.star, equipData.maxStar);
            int starIndex = star - 1;
            int skillCid = starIndex < cfgStar.SkillIds.Count ? cfgStar.SkillIds[starIndex] : -1;
            if (skillCid != -1)
            {
                hsSkillCid.Add(skillCid);
            }
        }

        private void ShowAttribute()
        {
            int cnt = this.listAttributeData.Count;
            this.attributes.RefillCells(cnt);
            this.ui.noAttr.SetActive(cnt == 0);
        }

        private void OnAttributeChanged(UIN_CommonIconTitleValue item, int index)
        {
            var attrCid = this.listAttributeData[index];
            var attrVal = this.dicAttributeData[attrCid];
            CfgAttribute cfg = TableCenter.attribute.Get(attrCid);
            string strVal = APIAttributeUIUtils.GetStrAttributeValue(cfg.ShowType, attrVal);
            item.Show(cfg.Icon_fullPath, cfg.Name, strVal);
        }

        private void ShowSkillNameDesc()
        {
            int cnt = this.listSkillData.Count;
            this.skills.RefillCells(cnt);
            this.ui.noSkill.SetActive(cnt == 0);
        }

        private void OnSkillNameDescChanged(UIN_CommonTitleValue item, int index)
        {
            int skillCid = this.listSkillData[index];
            var cfgSkillLv = TableCenter.skillLevel.GetCfg(skillCid, 1);
            var cfgSkill = TableCenter.skill.Get(skillCid);
            item.Show(cfgSkillLv.EffectText, cfgSkill.Name);
        }
        #endregion methonds
    }
}
